/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.manage.service.impl;

import me.zhengjie.modules.manage.domain.GiftAccount;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.manage.repository.GiftAccountRepository;
import me.zhengjie.modules.manage.service.GiftAccountService;
import me.zhengjie.modules.manage.service.dto.GiftAccountDto;
import me.zhengjie.modules.manage.service.dto.GiftAccountQueryCriteria;
import me.zhengjie.modules.manage.service.mapstruct.GiftAccountMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2024-09-10
**/
@Service
@RequiredArgsConstructor
public class GiftAccountServiceImpl implements GiftAccountService {

    private final GiftAccountRepository giftAccountRepository;
    private final GiftAccountMapper giftAccountMapper;

    @Override
    public Map<String,Object> queryAll(GiftAccountQueryCriteria criteria, Pageable pageable){
        Page<GiftAccount> page = giftAccountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(giftAccountMapper::toDto));
    }

    @Override
    public List<GiftAccountDto> queryAll(GiftAccountQueryCriteria criteria){
        return giftAccountMapper.toDto(giftAccountRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GiftAccountDto findById(String id) {
        GiftAccount giftAccount = giftAccountRepository.findById(id).orElseGet(GiftAccount::new);
        ValidationUtil.isNull(giftAccount.getId(),"GiftAccount","id",id);
        return giftAccountMapper.toDto(giftAccount);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GiftAccountDto create(GiftAccount resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return giftAccountMapper.toDto(giftAccountRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GiftAccount resources) {
        GiftAccount giftAccount = giftAccountRepository.findById(resources.getId()).orElseGet(GiftAccount::new);
        ValidationUtil.isNull( giftAccount.getId(),"GiftAccount","id",resources.getId());
        giftAccount.copy(resources);
        giftAccountRepository.save(giftAccount);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            giftAccountRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<GiftAccountDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GiftAccountDto giftAccount : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名字", giftAccount.getName());
            map.put("礼帐类型", giftAccount.getType());
            map.put("时间", giftAccount.getDate());
            map.put("方式", giftAccount.getWay());
            map.put("关系", giftAccount.getRelationship());
            map.put("是否已还", giftAccount.getIsReturn());
            map.put(" otherContent",  giftAccount.getOtherContent());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}